package com.chris.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class JsonProcess {
    public static JSONObject respError(String message){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",400);
        jsonObject.put("message",message);
        return jsonObject;
    }
    public static JSONObject respSuccess(String message,Object objects){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",200);
        jsonObject.put("message",message);
        jsonObject.put("data",objects);
        return  jsonObject;
    }
    public static JSONObject respSuccess(String message,Object objects,int total){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",200);
        jsonObject.put("message",message);
        jsonObject.put("data",objects);
        jsonObject.put("total",total);
        return  jsonObject;
    }
    public static JSONObject read(InputStream in) throws IOException {
        var in1 = new InputStreamReader(in, StandardCharsets.UTF_8);
        StringBuilder result = new StringBuilder();
        int data = 0;
        while ((data = in1.read())!=-1){
            result.append((char) data);
        }
        in1.close();
        return JSONObject.parseObject(result.toString());
    }
}
